$(function () {
    //分类更多
    sortMoreFun();
    //加载专业
    findSubjectList(function (result) {
        var allSubject = result;
        var parentSubjectList = allSubject[0];
        if (parentSubjectList != null && parentSubjectList.length > 0) {
            var context = '<li data-id="" class="cm-filter-item parent-subject active">全部</li>';
            $.each(parentSubjectList, function (i, e) {
                context += '<li data-id="' + e.id + '" class="cm-filter-item parent-subject">' + e.subjectName + '</li>';
            });
            $("#subject-list-parent").html(context);
            //分类更多
            sortMoreFun();
            //点击父级专业
            $("body").on('click', ".parent-subject", function () {
                var _this = $(this);
                _this.addClass('active').siblings('#subject-list-parent>.cm-filter-item').removeClass('active');
                var parentId = _this.data('id');
                $("#subjectId").val(parentId);
                queryCourseListData(1);
                var childList = allSubject[parentId];
                if (childList != null && childList.length > 0) {
                    $("#child-subject-list").removeClass('hide');
                    context = '<li data-id="' + parentId + '" class="cm-filter-item active">全部</li>';
                    $.each(childList, function (index, em) {
                        context += '<li class="cm-filter-item" data-id="' + em.id + '">' + em.subjectName + '</li>';
                    });
                    $("#child-subject-list ul").html(context);
                    sortMoreFun();
                    //点击子级专业
                    $("#child-subject-list ul>li").click(function () {
                        var _th = $(this);
                        var _subjectId = _th.data('id');
                        _th.addClass('active').siblings('#child-subject-list li').removeClass('active');
                        $("#subjectId").val(_subjectId);
                        queryCourseListData(1);
                    });
                    sortMoreFun();
                } else {
                    $("#child-subject-list").addClass('hide');
                }
            });
        }else{
            $("#subject-list-parent").parent().parent().parent().addClass('hide');
        }
    });

    /**
     * 获取讲师数据列表
     */
    findAllTeacherList(function(teacherList){
        if(teacherList!=null && teacherList.length>0){
            var context = '';
            $.each(teacherList,function (index,teacher) {
                context +='<li class="cm-filter-item" data-id="'+teacher.id+'">'+teacher.teacherName+'</li>';
            });
            $("#teacher-list").append(context);
            sortMoreFun();
        }else{
            $("#teacher-list").parent().parent().parent().addClass('hide');
        }
    },"1");

    $("body").on('click', "#course-type>li", function () {
        var _this = $(this);
        _this.addClass('active').siblings('#course-type>li').removeClass('active');
        queryCourseListData(1);
    }).on('click', "#year-list>li", function () {
        var _this = $(this);
        _this.addClass('active').siblings('#year-list>li').removeClass('active');
        queryCourseListData(1);
    }).on('click', "#teacher-list>li", function () {
        var _this = $(this);
        _this.addClass('active').siblings('#teacher-list>li').removeClass('active');
        queryCourseListData(1);
    }).on('click', ".order-by-box", function () {
        var order = $(this).data("order");
        if(order==1){
            $(this).data("order",2);
        }else if(order==2){
            $(this).data("order",1);
        }else if(order==3){
            $(this).data("order",4);
        }else if(order==4){
            $(this).data("order",3);
        }
        $("#order").val(order);
        $(".order-by-box").removeClass("c-danger").addClass("c-333");
        $(this).removeClass('c-333').addClass('c-danger');
        queryCourseListData(1);
    }).on('click', ".updateTimeOrderBy", function () {
        var updateTimeOrderBy = $(this).data("orderby");
        $("#updateTimeOrderBy").val(updateTimeOrderBy);
        if(updateTimeOrderBy==2){
            $(this).data("orderby","4");
        }else if(updateTimeOrderBy==4){
            $(this).data("orderby","2");
        }
        $("#priceOrderBy").val(0);
        $(this).removeClass('c-333').addClass('c-danger');
        $(".priceOrderBy").removeClass('c-danger').addClass('c-333');
        queryCourseListData(1);
    }).on('click', 'div[data-course]', function () {
        var courseId = $(this).data('course');
        window.location = baseloaction + '/web/course/courseInfo?courseId=' + courseId;
    });

    //获取课程页面数据列表
    function queryCourseListData(pageNo) {
        var params = {};
        var subjectIds = $("#subjectId").val();
        params['subjectIds'] = subjectIds;
        var courseName = $("#courseName").val();
        params['courseName'] = courseName;
        var courseType = $("#course-type .active").data('type');
        params['courseTypeKey'] = courseType;
        var teacherId = $("#teacher-list .active").data('id');
        params['teacherIds'] = teacherId;
        var year = $("#year-list .active").data('year');
        params['year'] = year;
        var order = $("#order").val();
        params['orderType'] = order;
        params = JSON.stringify(params);
        getPageAjaxData({
            url: baseloaction + '/web/course/ajaxCourseList',
            contextId: 'course-list-context',
            pageNo: pageNo,
            pageCall: queryCourseListData,
            params: params,
            callback: function (_pageNo) {
                $("#course-sum-box").text($("#course-total").val());
                lazyCourseImage();
            }
        });
    }
    queryCourseListData(1);
});
